﻿Imports System.Data.SqlClient

Public Class frmProTypeAct

    Private Sub btncancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btncancel.Click
        Try
            Me.Close()
            With frmProType
                .bindData("")
                .FormBorderStyle = Windows.Forms.FormBorderStyle.None
                .TopLevel = False
                .Dock = DockStyle.Fill
                frmMain.pnBody.Controls.Add(frmProType)
                .Show()
            End With
        Catch ex As Exception
            MessageBox.Show("Errors: " + ex.ToString())
        End Try
    End Sub

    Private Shared Function validatetype(ByVal typename As String, ByVal typeid As String)
        Try
            Dim typehave = dbconnection.ExecuteScalar("select TypeId, TypeName from Type where TypeName=N'" + typename + "' and TypeId<>'" + typeid + "';")
            Return typehave
        Catch ex As Exception
            MessageBox.Show("Errors: " + ex.ToString)
            Return False
        End Try
    End Function

    Private Sub btnsave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsave.Click
        If (validatetype(txtProType.Text, lblTypeID.Text) IsNot Nothing) Then
            MessageBox.Show("Errors: Product Type Already Exists Please Try With Other...!")
            txtProType.Focus()
        Else
            If (txtProType.Text = "") Then
                MessageBox.Show("Errors: Please complete Product Type to continues...!")
                txtProType.Focus()
            Else
                If (lblTypeID.Text = "") Then
                    Try
                        Dim cmdadd As SqlCommand
                        cmdadd = dbconnection.Connection.CreateCommand
                        With cmdadd
                            .CommandType = CommandType.Text
                            .CommandText = "insert into Type (TypeName, Active) values (@typename, 1);"
                            .Parameters.Add("@typename", SqlDbType.NVarChar).Value = txtProType.Text.Trim()
                            .ExecuteNonQuery()
                        End With
                        MessageBox.Show("Record Added")
                        Me.Close()
                        With frmProType
                            .bindData("")
                            .FormBorderStyle = Windows.Forms.FormBorderStyle.None
                            .TopLevel = False
                            .Dock = DockStyle.Fill
                            frmMain.pnBody.Controls.Add(frmProType)
                            .Show()
                        End With
                    Catch ex As Exception
                        MessageBox.Show("Errors " + ex.ToString)
                    End Try
                Else
                    Try
                        Dim cmdadd As SqlCommand
                        cmdadd = dbconnection.Connection.CreateCommand
                        With cmdadd
                            .CommandType = CommandType.Text
                            .CommandText = "update Type set TypeName=@typename where TypeId=@typeid;"
                            .Parameters.Add("@typename", SqlDbType.NVarChar).Value = txtProType.Text.Trim()
                            .Parameters.Add("@typeid", SqlDbType.NVarChar).Value = lblTypeID.Text.Trim()
                            .ExecuteNonQuery()
                        End With
                        MessageBox.Show("Record Updated")
                        Me.Close()
                        With frmProType
                            .bindData("")
                            .FormBorderStyle = Windows.Forms.FormBorderStyle.None
                            .TopLevel = False
                            .Dock = DockStyle.Fill
                            frmMain.pnBody.Controls.Add(frmProType)
                            .Show()
                        End With
                    Catch ex As Exception
                        MessageBox.Show("Errors " + ex.ToString)
                    End Try
                End If
            End If
        End If
    End Sub
End Class